package com.example.practice4.controller;

import com.example.practice4.Entity.Course;
import com.example.practice4.Entity.Student;
import com.example.practice4.repository.CourseRepository;
import com.example.practice4.repository.StudentRepository;
import com.example.practice4.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("StudentInfo")
public class DesignController {
    private Student s;
    private List<Course> checkList;
    private String username;

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private CourseRepository courseRepository;
    @Autowired
    private StudentRepository studentRepository;

    @GetMapping("/addStudentInfo")
    public String addStudentInfo(Model model){
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();

        //获取登录用户名字
        username=userDetails.getUsername();
        System.out.println(username);

        //跳转到学生个人信息页面
        Student s=new Student();
        model.addAttribute("StudentInfo",s);

        //存取登录的用户名


        model.addAttribute("username",username);
        return "StudentInfo";
    }

    @GetMapping("/selectCourse")
    public String selectCourse(@Valid @ModelAttribute("StudentInfo") Student student, Errors errors, Model model){

        if(errors.hasErrors()){
            return "StudentInfo";
        }
        //  System.out.println(student.toString());


        //实例化学生对象
        s=new Student();
        s=student;

        List<Course> list=courseRepository.queryCourses();


        System.out.println(list);

        model.addAttribute("StudentInfo",s);
        model.addAttribute("courses",list);
        model.addAttribute("username",username);
        System.out.println(list);
        return "selectCourse";
    }

    @PostMapping("/submitCourse")
    public String showResult(Model model, HttpServletRequest request){
        checkList=new ArrayList<>();
        String[] checkCourses=request.getParameterValues("checkCourses");

        for(int i=0;i<checkCourses.length;i++) {
            checkList.add(courseRepository.queryByName(checkCourses[i]));
        }
        s.setCourses(checkList);

        //保存学生对象和已选课程信息到数据库
        studentRepository.save(s);
        System.out.println(s);

        model.addAttribute("StudentInfo",s);
        model.addAttribute("courses",checkList);
        model.addAttribute("username",username);

        return "FinalResult";
    }


    }
